package app;


import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.Timer;

import utils.BasicFileReader;
import addons.GestioneErrori;
import data.Coordinate;
import data.Game;
import data.GameLoop;
import data.PlayZone;
import data.Player;
import events.Action;

/**
 * Classe per mettere a posto le abilita le squadre dei giocatori. Da mettere a
 * posto i layout che per adesso non so come gestirli.
 * 
 * @author TeamVulcan
 * 
 */
public class SecondMenu extends JPanel {
	private ArrayList<PlayerPane> players = new ArrayList<PlayerPane>();
	private JButton addNew, goBack, exit, play;
	private Container top;
	private JToolBar down;
	private PlayZone playZone;
	private GameLoop gameLoop;
	private JFrame game;
	private int cicles = 0;
	private final static int SIZE_SETUP = 70;
	private final static int MAX_HP = 5;
	private final static int MAX_EP = 100;

	public SecondMenu() {
		super();
		setLayout(new BoxLayout(this, 3));
		top = new JPanel();
		down = new JToolBar();
		down.setEnabled(false);
		getMainPlayerInfo(); // crea il player
		GenerateNPC(); // crea il primo nemico
		top.setLayout(new BoxLayout(top, 3));
		top.setFocusable(false);

		addNew = new JButton("Add another computer");
		addNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				GenerateNPC();
			}
		});
		goBack = new JButton("Back to Main Menu");
		goBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				PlayerPane.resetNPCcount();
				GameFrame.getFrame();
				GameFrame.refreshMainMenu();
			}
		});
		play = new JButton("Play");
		play.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				playZone = new PlayZone(500, 500); // (Height,Width)
				CreatePlayZone();
				gameLoop = new GameLoop(playZone.getPlayers());
				players.removeAll(players);
				/**
				 * Qui serve creare tutti i giocatori e i ostacoli se ci sono. e
				 * certamente aggiungerli al pz.
				 */
				Game.getSingletonGame().setPlayZone(playZone);
				Action action = new Action(playZone.getPlayers().get(0));
				gameLoop.setFocusable(true);
				gameLoop.addKeyListener(action.getPa());
				action.getDa().addObserver(gameLoop);
				gameLoop.addMouseListener(action.getDa());
				GameFrame.getFrame().setVisible(false);
				SecondMenu.this.game = new JFrame();
				SecondMenu.this.game.setTitle("Game");
				SecondMenu.this.game
						.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				SecondMenu.this.game.getContentPane().add(gameLoop);
				SecondMenu.this.game.setCursor(Cursor
						.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
				SecondMenu.this.game.setSize((playZone.getWidth()) + 90,
						(playZone.getHeight()) + 100);
				SecondMenu.this.game.setLocationRelativeTo(null);
				SecondMenu.this.game.setVisible(true);
				ActionListener actionListener = new ActionListener() {
					public void actionPerformed(ActionEvent actionEvent) {
						Game.getSingletonGame().getPlayZone().moveDarts();
						if (cicles % 4 == 0)
							Game.getSingletonGame().getPlayZone()
									.restoreAPandEP();
						SecondMenu.this.cicles++;
						SecondMenu.this.gameLoop.repaint();
					}
				};
				Timer timer = new Timer(200, actionListener);
				timer.start();
			}
		});
		exit = new JButton("Exit");
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				GameFrame.getFrame().dispose();
			}
		});

		down.add(addNew);
		down.add(play);
		down.add(goBack);
		down.add(exit);
		add(down);
		add(top);
	}

	// Metodo per otenere da un file le info del player come l'abilita
	// prescelta,il team,il nome player
	private void getMainPlayerInfo() {
		BasicFileReader reader = new BasicFileReader("save.txt");
		PlayerPane player = new PlayerPane();
		player.makePlayerPane();
		String buff = null;

		if (reader != null) {
			buff = reader.readLine();
			player.getPname().setText(buff);
			buff = reader.readLine();
			player.getPteam().select(Integer.parseInt(buff) - 1);
			buff = reader.readLine();
			player.getPability().select(Integer.parseInt(buff) - 1);
			reader.close();
		}
		players.add(player);
		top.add(player);
		Dimension d = new Dimension();
		d.setSize(top.getWidth() + 10, top.getHeight() + down.getHeight()
				+ SIZE_SETUP);
		if (d.getHeight() != 0)
			GameFrame.getFrame().setSize(d);
	}

	// usa il MakeNPC del PlayerPane
	private void GenerateNPC() {
		PlayerPane npc = new PlayerPane();
		npc.MakeNPC();
		players.add(npc);
		top.add(npc);
		top.repaint();
		SecondMenu.this.repaint();
		Dimension d = new Dimension();
		d.setSize(top.getWidth() + 50, top.getHeight() + down.getHeight()
				+ SIZE_SETUP);
		if (d.getHeight() != 0)
			GameFrame.getFrame().setSize(d);
	}

	// Metodo per ottenere il pz da oggetti grafici
	// Alcune cose sono da iniziare come default
	private void CreatePlayZone() {
		Player player = new Player();
		ArrayList<Coordinate> coordArray = GenerateCoordinate();
		int i = 0;
		if (players.size() > 4)
			GestioneErrori.getGestore().reportError(3);
		do {
			player.setName(players.get(i).getPname().getText());
			player.setTeam((players.get(i).getPteam()).getSelectedIndex() + 1);
			// + cosi il team inizia da 1
			player.setAbilita(players.get(i).getPability().getSelectedIndex());
			if (player.getAbilitaReturnComeParametroUnaAbilitaNonUnIntero() == 0) {
				player.setAbilita(new Random().nextInt(4) + 1);
			}
			// p.setMAX_ENERGY(MAXEP);
			player.setEnergy(MAX_EP);
			player.setHealthPoints(MAX_HP);
			player.setPosition(coordArray.get(i));
			playZone.addPlayer(player);
			i++;
			player = new Player();
			// aggiunto per il player massimo numero 4
			if (i == 4)
				break;
		} while (i < players.size());
	}

	
	// Algoritmo per generare le coordinate non random

	private ArrayList<Coordinate> GenerateCoordinate() {
		ArrayList<Coordinate> coordArray = new ArrayList<Coordinate>();
		int x = 0, y = 0;

		for (int i = 0; i < players.size(); i++) {
			switch (i) {
			case 0:
				x = 0;
				y = 0;
				break;
			case 1:
				y = playZone.getHeight();
				x = playZone.getWidth();
				break;
			case 2:
				y = 0;
				x = playZone.getWidth();
				break;
			case 3:
				y = playZone.getHeight();
				x = 0;
				break;

			default:
				break;
			}
			coordArray.add(new Coordinate(x, y));
		}
		return coordArray;
	}

}
